<?php

namespace app\modules\groups\models\query;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\modules\groups\models\Group;

/**
 * GroupQuery represents the model behind the search form about `app\modules\groups\models\Group`.
 */
class GroupQuery extends Group
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['groupID', 'parentGroupID', 'oldGroupID', 'status'], 'integer'],
            [['groupName', 'groupCode', 'groupDesc', 'group_mark', 'createTime', 'updateTime', 'createUser', 'updateUser', 'groupStr'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Group::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'groupID' => $this->groupID,
            'parentGroupID' => $this->parentGroupID,
            'oldGroupID' => $this->oldGroupID,
            'status' => $this->status,
            'createTime' => $this->createTime,
            'updateTime' => $this->updateTime,
        ]);

        $query->andFilterWhere(['like', 'groupName', $this->groupName])
            ->andFilterWhere(['like', 'groupCode', $this->groupCode])
            ->andFilterWhere(['like', 'groupDesc', $this->groupDesc])
            ->andFilterWhere(['like', 'group_mark', $this->group_mark])
            ->andFilterWhere(['like', 'createUser', $this->createUser])
            ->andFilterWhere(['like', 'updateUser', $this->updateUser])
            ->andFilterWhere(['like', 'groupStr', $this->groupStr]);

        return $dataProvider;
    }
}
